package tomcatDemo;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer {
	public static void main(String[] args) throws Exception{
		ServerSocket serverSocket = null;
		Socket socket = null;
		OutputStream ops = null;
		try {
			//1_创建ServerSokcet对象，监听8080端口
			serverSocket = new ServerSocket(8080);
			while(true){
				socket = serverSocket.accept();
				ops = socket.getOutputStream();
				ops.write("HTTP/1.1 200 OK\n".getBytes());
				ops.write("Content-Type:text/html;charset=utf-8\n".getBytes());
				ops.write("Server:Apache-Coyote/1.1\n".getBytes());
				ops.write("\n\n".getBytes());
				StringBuffer sb = new StringBuffer();
				sb.append("<html>");
				sb.append("<title>测试服务器</title>");
				sb.append("<head></head>");
				sb.append("<body><center><h1>I Love You, MM</h1></center></body>");
				sb.append("</html>");
				ops.write(sb.toString().getBytes());
				ops.flush();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			if(null != ops){
				ops.close();
				ops = null;
			}
			if(null != socket){
				socket.close();
				socket = null;
			}
		}
	}
}
